package com.insta.formation.calendar.service.ws;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import com.insta.formation.calendar.manager.ICalendarManager;
import com.insta.formation.calendar.manager.IUserManager;
import com.insta.formation.calendar.model.Calendar;
import com.insta.formation.calendar.model.Event;
import com.insta.formation.calendar.model.Person;
import com.insta.formation.calendar.service.CalendarLight;
import com.insta.formation.calendar.service.EventLight;
import com.insta.formation.calendar.service.IVisitorService;

/**
 *  @See {@link IVisitorService}
 */
@WebService(endpointInterface = "com.insta.formation.calendar.service.IVisitorService")
public class VisitorServiceWS implements IVisitorService
{
	private IUserManager userManager;
	private ICalendarManager calendarManager;
	
	public VisitorServiceWS(IUserManager userManager, ICalendarManager calendarManager) {
		this.userManager = userManager;
		this.calendarManager = calendarManager;
	}
	
	/**
	 *  @See {@link IVisitorService}
	 */
	public EventLight visitEvent(String calendarName, String eventName, String emailGuest) {
		Event event = calendarManager.visitEvent(calendarName, eventName, emailGuest);
		if (event != null) {
			EventLight eventLight = new EventLight();
			eventLight.setName(event.getName());
			eventLight.setCalendarName(calendarName);
			eventLight.setStartDate(event.getStartDate());
			eventLight.setEndDate(event.getEndDate());

			List<String> emails = new ArrayList<String>();
			for (Person guest : event.getGuests()) {
				emails.add(guest.getEmail());
			}
			eventLight.setGuest(emails);
			return eventLight;
		}
		return null;
	}

	/**
	 *  @See {@link IVisitorService}
	 */
	public CalendarLight visitCalendar(String calendarName) {
		Calendar calendar = calendarManager.retreiveCalendar(calendarName);
		if (calendar != null) {
			CalendarLight calendarLight = new CalendarLight();
			calendarLight.setName(calendarName);
			
			List<String> events = new ArrayList<String>();
			for (Event event : calendar.getEvents()) {
				events.add(event.getName());
			}
			calendarLight.setEvents(events);
			
			return calendarLight;
		}
		return null;
	}

}
